package com.robot.agent.util;

import entity.StatusCode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import util.CheckUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息推送
 *
 * @author lin
 */
@Component
public class SendMessageUtil {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private CheckUtil checkUtil;
    /**
     * 消息推送type 0-短信 1-站内信 2-邮件 3-微信
     */
    private int phoneMessagePush = 0;
    private int mailMessagePush = 1;
    private int emailMessagePush = 2;
    private int weChatMessagePush = 3;
    /**
     * 消息推送内容长度
     */
    @Value("${sys.messagePushContentLength}")
    private int messagePushContentLength;

    /**
     * 消息推送
     *
     * @param map
     * @return
     */
    public Map<String, Object> sendMessage(Map map) {
        HashMap<String, Object> hashMap = new HashMap<>(3);
        Integer type = (Integer) map.get("type");
        if (type == null) {
            hashMap.put("flag", false);
            hashMap.put("code", StatusCode.ERROR);
            hashMap.put("message", "场景值必填");
            return hashMap;
        }

        String content = (String) map.get("content");
        if (content == null || "".equals(content)) {
            hashMap.put("flag", false);
            hashMap.put("code", StatusCode.ERROR);
            hashMap.put("message", "消息内容必填");
            return hashMap;
        }
        if (content.length() > messagePushContentLength) {
            hashMap.put("flag", false);
            hashMap.put("code", StatusCode.ERROR);
            hashMap.put("message", "消息内容不超过50个字");
            return hashMap;
        }

        if (phoneMessagePush == type) {
            String phone = (String) map.get("phone");
            if (!checkUtil.checkPhone(phone)) {
                hashMap.put("flag", false);
                hashMap.put("code", StatusCode.ERROR_INVALID_PHONE);
                hashMap.put("message", "请输入正确的手机号");
                return hashMap;
            }
            sendSms(phone, content);
        } else if (emailMessagePush == type) {
            String receiver = (String) map.get("receiver");
            if (checkUtil.checkEmail(receiver)) {
                hashMap.put("flag", false);
                hashMap.put("code", StatusCode.ERROR);
                hashMap.put("message", "请输入正确的邮箱账号");
                return hashMap;
            }
            String subject = (String) map.get("subject");
            if (subject == null || "".equals(subject)) {
                hashMap.put("flag", false);
                hashMap.put("code", StatusCode.ERROR);
                hashMap.put("message", "邮件主题必填");
                return hashMap;
            }
            sendEmail(receiver, subject, content);
        } else if (mailMessagePush == type) {

        } else if (weChatMessagePush == type) {

        } else {
            hashMap.put("flag", false);
            hashMap.put("code", StatusCode.ERROR);
            hashMap.put("message", "消息推送时场景错误");
            return hashMap;
        }
        hashMap.put("flag", true);
        hashMap.put("code",StatusCode.SUCCESS);
        hashMap.put("message", "消息推送成功");
        return hashMap;
    }

    /**
     * 发送短信内容推送
     *
     * @param phone
     * @param content
     */
    public void sendSms(String phone, String content) {
        Map<String, String> map = new HashMap<>(2);
        map.put("phone", phone);
        map.put("content", content);
        /**
         * 为方便测试，先不实现短信的发送，直接在后台显示
         */
        rabbitTemplate.convertAndSend("message_push", map);
        /**
         * 在控制台显示一份【方便测试】
         */
        System.out.println("the phone is:" + phone + ",the content is:" + content);
    }

    /**
     * 发送邮箱文本消息推送
     */
    @Autowired
    JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String sender;

    @Async
    public void sendEmail(String receiver, String subject, String content) throws MailSendException {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(subject);
        message.setText(content);
        message.setTo(receiver);
        message.setFrom(sender);

        System.out.println("收件人:" + receiver + "发件人:" + sender + ",主题:" + subject + ",内容:" + content);
        mailSender.send(message);
    }

}
